﻿using System;
using System.Windows.Controls;
using Plugins.Base.Historical;
using Plugins.Core;
using StockAccessors;
using StockModel;
using Utils;

namespace Plugins.Base.Common
{
	public abstract class PluginBase : IStrategyPlugin
	{
		#region Attributes

		private HistoricalEngineBase _historicalEngine;

		#endregion

		#region Properties

		public abstract bool SupportsRealtime { get; }

		public UserControl HistoricalControlPanel { get; protected set; }
		public UserControl RealtimeControlPanel { get; protected set; }
		public VisualRepresentation VisualRepresentation { get; protected set; }

		protected Historical.HistoricalEngineBase HistoricalEngine
		{
			get
			{
				return _historicalEngine;
			}
			set
			{
				_historicalEngine = value;
				_historicalEngine.ResultsAvailable += HistoricalEngine_OnResultsAvailable; // TODO: Disposed but its not a priority because this will be disposed only on application shutdown
			}
		}

		protected RealTime.RealTimeEngineBase RealTimeEngine { get; set; }

		#endregion

		#region Events

		public event EventHandler<HistoricalResultsAvailableEventArgs> HistoricalResultsAvailable;
		public event EventHandler<RealtimeNotificationEventArgs> RealtimeNotification;
		
		#endregion
		
		#region Public Methods

		public abstract void Initialize(IDataBaseClient databaseClient, QuotationDataSource quotationDataSource, ExchangeMarketDataSource exchangeMarketDataSource, RawStockDataSource rawStockDataSource, ObservableCollection2<StockGroup> realtimeGroups, IOrderExecutor executor, IStockDataAccessorInformation accessorInformation);

		protected virtual void OnResultsAvailable(HistoricalResultsAvailableEventArgs e)
		{
			if (this.HistoricalResultsAvailable != null)
			{
				this.HistoricalResultsAvailable(this, e);
			}
		}

		protected virtual void OnRealtimeNotification(RealtimeNotificationEventArgs e)
		{
			if (this.RealtimeNotification != null)
			{
				this.RealtimeNotification(this, e);
			}
		}

		#endregion

		#region Event Handlers

		private void HistoricalEngine_OnResultsAvailable(object sender, HistoricalResultsAvailableEventArgs e)
		{
			this.OnResultsAvailable(e);
		}

		#endregion
	}
}
